<?php

namespace App\Services;

use App\Models\Mini\NoticeModel;

class NoticeService extends BaseService
{
    /**
     * Notes: 获取最新的公告
     * User: lstink
     * Date: 2021/3/4
     * Time: 3:08 下午
     * @return array
     */
    public function getNewNotice(): array
    {
        //获取认证用户信息
        $user_info = auth('user')->user();
        $now       = strtotime(date('Y-m-d'));
        $whereRaw = 'find_in_set('.$user_info['community_id'].',community_id)';
        /*$list = NoticeModel::where(['estate_id' => $user_info['property_id'],'is_show'=>1,'status'=>1])
            ->where([['start_time', '<=', $now], ['end_time', '>=', $now]])
            ->whereRaw($whereRaw)
            ->OrWhere(function ($query) use ($user_info) {
                $query->where(['estate_id' => $user_info['property_id']])->where('end_time', 0);
            })->orderBy('create_time', 'desc')->get();*/
        $list = NoticeModel::where(['is_show'=>1,'status'=>1,'estate_id' => $user_info['property_id']])  //物业自己；录入的未删除和未禁用的状态
                ->where(function ($query) use ($now,$user_info) {
                    $query->where([['start_time', '<=', $now], ['end_time', '>=', $now]])  //有效时间内
                    ->OrWhere(['end_time' =>0]);  //长期的数据
                })
                ->whereRaw($whereRaw)
                ->orderBy('create_time', 'desc')->get();

        return $list ? $this->format($list) : $this->format_error('暂无公告');

    }

    public function gePageNotice($pre_page): array
    {
        //获取认证用户信息
        $user_info = auth('user')->user();
        $now       = strtotime(date('Y-m-d'));
        $whereRaw = 'find_in_set('.$user_info['community_id'].',community_id)';
        $list = NoticeModel::where(['is_show'=>1,'status'=>1,'estate_id' => $user_info['property_id']])  //物业自己；录入的未删除和未禁用的状态
            ->with('one_estate')
            ->where(function ($query) use ($now,$user_info) {
                $query->where([['start_time', '<=', $now], ['end_time', '>=', $now]])  //有效时间内
                ->OrWhere(['end_time' =>0]);  //长期的数据
            })
            ->whereRaw($whereRaw)
            ->orderBy('create_time', 'desc')
            ->paginate($pre_page ?? 10)
            ->toArray();
        return $list ? $this->format($list) : $this->format_error('暂无公告');

    }



    /**
     * Notes: 详情
     * User: lstink
     * Date: 2021/3/4
     * Time: 3:37 下午
     * @param $id
     * @return array
     */
    public function getNoticeDetail($id): array
    {
        $detail = NoticeModel::with('author')->where('id', $id)->first();
        return $detail ? $this->format($detail) : $this->format_error('暂无数据');
    }
}
